import React, {Component} from 'react';
import './index.less';
import {base, toast} from 'nc-lightapp-front';
import deleteRows from '../../request-functions/deleteBlackList';
import handleAllpks from "../../container/functions/handleAllpks";

const {NCModal, NCButton} = base;
const {Body: ModalBody, Footer: ModalFooter} = NCModal;

class DeleteConfirmModal extends Component {
    constructor(props) {
        super(props);

        this.beSure = this.beSure.bind(this);
    }

    async beSure() {
        const {pk_psndoc_bad, orgVal, page, getMainTableData, getOnePsnInfo, updateState, cardPagination} = this.props;
        if (pk_psndoc_bad) {
            try {
                let res = await deleteRows({
                    pk_org: orgVal && orgVal.refpk,
                    pk_psndoc_bad: pk_psndoc_bad
                });
                if (res.success) {
                    toast({
                        color: 'success',
                        content: this.props.json['hi6007-000006']/* 国际化处理： 删除成功*/
                    });
                    if (page === 'main') {
                        getMainTableData();
                        updateState({
                            deleteConfirmModalVisible: false
                        });
                    } else {
                        const nextBad = cardPagination.getNextCardPaginationId({
                            id: pk_psndoc_bad,
                            status: 1
                        });
                        handleAllpks.remove(pk_psndoc_bad);
                        if(nextBad){
                            updateState({
                                pk_psndoc_bad: nextBad,
                                deleteConfirmModalVisible: false
                            }, () => {
                                getOnePsnInfo();
                            });
                        }else{
                            updateState({
                                pk_psndoc_bad: '',
                                deleteConfirmModalVisible: false
                            }, () => {
                                getOnePsnInfo();
                            });
                        }
                    }
                }
            }
            catch (e) {
            }
        }
    }

    render() {
        const {
            deleteConfirmModalVisible
        } = this.props;

        return (
            <NCModal
                show={deleteConfirmModalVisible}
                size="sm"
                backdrop={'static'}
            >
                <ModalBody>
                    <div>{this.props.json['hi6007-000007']}？</div>{/* 国际化处理： 您确定要删除所选数据吗*/}
                </ModalBody>
                <ModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.beSure}
                    >
                        {this.props.json['hi6007-000004']}{/* 国际化处理： 确定*/}
                    </NCButton>
                    <NCButton
                        onClick={() => {
                            this.props.updateState({
                                deleteConfirmModalVisible: false
                            });
                        }}
                    >
                        {this.props.json['hi6007-000005']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </ModalFooter>
            </NCModal>
        );
    }
}

export default DeleteConfirmModal;
